package com.alibaba.middleware.race.sync.util;

import static com.alibaba.middleware.race.sync.util.TestServer.SEP;
import static com.alibaba.middleware.race.sync.util.TestServer.SEP2;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yh on 2017/6/6.
 *
 *
 */
public class ResultRs {
    // |mysql-bin.0000014284|1496413874000|test|user|I|id:1:1|NULL|1|name:2:0|NULL|mark|age:1:0|NULL|18|
   
 
    public Map<String, Long> longColums = new HashMap<>();
    public Map<String, String> stringColums = new HashMap<>();

    public void updateData(final byte[] dataBytes, int start) {
        int position = start;
        int dataPosition = position;
    
        while (position < dataBytes.length) {
            String key;
            boolean type;
     
                while (dataBytes[position++] != SEP2) {}
                key = new String(dataBytes, dataPosition, position - dataPosition - 1);
                if (dataBytes[position] == "1".getBytes()[0]) {
                    type = true;
                } else {
                    type = false;
                }
                dataPosition = position = position + 4;
                while (dataBytes[position++] != SEP) {}
                String oldDataStr = new String(dataBytes, dataPosition, position - dataPosition - 1);
                if (!oldDataStr.equals("NULL")) {
                    if (type) {
                        long columsData = Long.decode(oldDataStr);
                        if (!longColums.containsKey(key)) {
                            longColums.put(key, columsData);
                        }
                    } else {
                        if (!stringColums.containsKey(key)) {
                            stringColums.put(key, oldDataStr);
                        }
                    }
                    while (dataBytes[position++] != SEP) {}
                    dataPosition = position;
                    continue;
                }
                dataPosition = position;
                while (dataBytes[position++] != SEP) {}
                String newDataStr = new String(dataBytes, dataPosition, position - dataPosition - 1);
                if (type) {
                    long columsData = Long.decode(newDataStr);
                    if (!longColums.containsKey(key)) {
                        longColums.put(key, columsData);
                    }
                } else {
                    if (!stringColums.containsKey(key)) {
                        stringColums.put(key, newDataStr);
                    }
                }
                dataPosition = position;
          

        }

//            while (dataBytes[position++])
    }

    public void clearDatra() {
        longColums.clear();
        stringColums.clear();
    }

//    public ResultRs(final String line) {
//        final String[] datas = line.split("\\|");
//
//        timeStamp = Long.valueOf(datas[2]);
//        schemaTable = datas[3] + datas[4];
//        type = datas[5];
//        if (type.equals("D")) {
//            longColums = null;
//            stringColums = null;
//        } else {
//            for (int i = 6; i < datas.length;i++) {
//                final String[] columData = datas[i++].split(":");
//                if (columData[2].equals("1")) {
//                    pramaryKey = Long.valueOf(datas[++i]);
//                } else {
//                    if (columData[1].equals("1")) {
//                        longColums.put(columData[0], Long.valueOf(datas[++i]));
//                    } else {
//                        stringColums.put(columData[0], datas[++i]);
//                    }
//                }
//            }
//        }
//    }



}
